﻿using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Microsoft.BizTalk.Tracking;

namespace Bizspoke.BizTalk.Brma
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct TrackedMessageInfo
    {
        public string SvcGUID;
        public string MessageGUID;
        public long MessageSize;
        public string MessageBoxName;
        public string MessageBoxSrv;
        public string Host;
        public string GroupName;
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct TrackedInstance
    {
        public string ServiceInstanceId;
        public string ActivityId;
        public List<TrackedMessage> TrackedMessages;
        public Dictionary<string, TrackingProperty> TrackingProperties;
        public string Status;

        public bool IsValid
        {
            get
            {
                return !string.IsNullOrEmpty(this.ServiceInstanceId) && (this.TrackedMessages.Count > 0);
            }
        }

        public TrackedInstance(string serviceInstanceId, string activityID)
        {
            this.ServiceInstanceId = serviceInstanceId;
            this.ActivityId = activityID;
            this.TrackedMessages = new List<TrackedMessage>();
            this.TrackingProperties = new Dictionary<string, TrackingProperty>();
            this.Status = string.Empty;
        }

        public bool HasTrackedMessage
        {
            get
            {
                if (this.TrackedMessages == null || this.TrackedMessages.Count == 0)
                    return false;

                TrackedMessage trkmsg = this.TrackedMessages.Find(delegate(TrackedMessage tm) { return tm.Status == "Saved"; });

                return (trkmsg.MessageInstanceId != null);
            }
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct TrackedMessage
    {
        public string MessageType;
        public string MessageInstanceId;
        public string Status;
        public long Size;

        public TrackedMessage(string messageInstanceId, string status, long size)
        {
            this.MessageType = null;
            this.MessageInstanceId = messageInstanceId;
            this.Status = status;
            this.Size = size;
        }
    }
}

